package com.sunxueqi.common.exception;

import com.sunxueqi.common.util.MessageUtils;

import cn.hutool.core.util.StrUtil;

public class BaseException extends RuntimeException{

	private static final long serialVersionUID = 1L;

	/**
	 * module名
	 */
	private String module;
	
	/**
	 * 错误码 error code
	 */
	private String code;
	
	/**
	 * 错误信息 error defaultMessage
	 */
	private String defaultMessage;
	
	/**
	 * 相关参数
	 */
	private Object[] args;
	
	public BaseException(String module, String code, String defaultMessage, Object[] args) {
		this.module = module;
		this.code = code;
		this.defaultMessage = defaultMessage;
		this.args = args;
	}
	
	public BaseException(String module, String code, Object[] args) {
		this(module, code, null,args);
	}
	
	public BaseException(String module, String defaultMessage) {
		this(module, null, defaultMessage, null);
	}
	
	public BaseException(String code, Object[] args) {
		this(null, code, null, args);
	}
	
	public BaseException(String defaultMessage) {
		this(null, null, defaultMessage, null);
	}
	
	@Override
	public String getMessage() {
		String message = null;
		if(StrUtil.isNotEmpty(code)) {
			message = MessageUtils.message(code, args);
		}
		if(message == null) {
			message = this.defaultMessage;
		}
		return message;
	}

	public String getModule() {
		return module;
	}

	public String getCode() {
		return code;
	}

	public Object[] getArgs() {
		return args;
	}
	
	public String getDefaultMessage() {
		return defaultMessage;
	}
	
}
